package com.zhende.hr.config;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

/**
 * mysql数据源配置
 * @author mk
 * @create 2019年7月29日 下午5:12:10
 */
@Configuration
@MapperScan(basePackages = { "com.zhende.hr.mapper.oaview"},sqlSessionFactoryRef = "db4SqlSessionFactory", sqlSessionTemplateRef = "db4SqlSessionTemplate")
public class DbOaViewConfig {
	
	private Logger log = LoggerFactory.getLogger(DbOaViewConfig.class);
	
	@Value("${spring.datasource.druid.four.url}")
	private String dbUrl;
	// oracle 账号
	@Value("${spring.datasource.druid.four.username}")
	private String username;
	// oracle 密码
	@Value("${spring.datasource.druid.four.password}")
	private String password;
	// oracle 驱动名
	@Value("${spring.datasource.druid.four.driverClassName}")
	private String driverClassName;
	// 初始化大小
	@Value("${spring.datasource.druid.four.initialSize}")
	private int initialSize;
	// 最小连接
	@Value("${spring.datasource.druid.four.minIdle}")
	private int minIdle;
	// 最大连接
	@Value("${spring.datasource.druid.four.maxActive}")
	private int maxActive;
	// 配置获取连接等待超时的时间
	@Value("${spring.datasource.druid.four.maxWait}")
	private int maxWait;
	// 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
	@Value("${spring.datasource.druid.four.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
	// 配置一个连接在池中最小生存的时间，单位是毫秒
	@Value("${spring.datasource.druid.four.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
	// 检验连接sql
	@Value("${spring.datasource.druid.four.validationQuery}")
	private String validationQuery;
	/**
	 * 起了一个 异步Evict的TimerTask定时线程进行控制 ( 可通过设置参数 timeBetweenEvictionRunsMillis>0),
	 * 定时对线程池中的链接进行validateObject校验， 对无效的链接进行关闭后，会调用ensureMinIdle，
	 * 适当建立链接保证最小的minIdle连接数
	 */
	@Value("${spring.datasource.druid.four.testWhileIdle}")
	private boolean testWhileIdle;
	// 进行borrowObject进行处理时，对拿到的connection进行validateObject校验
	@Value("${spring.datasource.druid.four.testOnBorrow}")
	private boolean testOnBorrow;
	// 进行returnObject对返回的connection进行validateObject校验，个人觉得对数据库连接池的管理意义不大
	@Value("${spring.datasource.druid.four.testOnReturn}")
	private boolean testOnReturn;
	// 打开PSCache，并且指定每个连接上PSCache的大小
	@Value("${spring.datasource.druid.four.poolPreparedStatements}")
	private boolean poolPreparedStatements;
	// 配置监控统计拦截的filters，去掉后监控界面sql无法统计，'wall'用于防火墙
	@Value("${spring.datasource.druid.four.filters}")
	private String filters;
	
    @Bean()
    @ConfigurationProperties(prefix = "spring.datasource.druid.four")
    public DataSource db4DataSource() {
         return DruidDataSourceBuilder.create().build();
    }
 
    // 3.4.0之前
//    @Autowired
//    private PaginationInterceptor paginationInterceptor;
    // 3.4.0之后
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
    @Bean
    public SqlSessionFactory db4SqlSessionFactory(@Qualifier("db4DataSource") DataSource dataSource) throws Exception {
    	MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
//      关键代码 设置 MyBatis-Plus 分页插件
        Interceptor[] plugins = {mybatisPlusInterceptor()};
        bean.setPlugins(plugins);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/oaview/*.xml"));
        return bean.getObject();
    }
 
    @Bean
    public DataSourceTransactionManager db4TransactionManager(@Qualifier("db4DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Bean
    public SqlSessionTemplate db4SqlSessionTemplate(@Qualifier("db4SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


	
}
